package com.kqb.sgpicturebackend.manager;

import com.kqb.sgpicturebackend.config.CosClientConfig;
import com.kqb.sgpicturebackend.model.dto.picture.PictureUploadRequest;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author 杉果树皮
 * @date 2025/4/28 14:08
 * @description:
 */
@Slf4j
@Service
public class CosManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private COSClient cosClient;


    /**
     * 将本地文件上传到 COS
     *
     * @param key  对象键(Key)是对象在存储桶中的唯一标识
     * @param file 本地文件
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        return cosClient.putObject(cosClientConfig.getBucket(), key, file);
    }


    /**
     * 从 COS 获取文件
     *
     * @param key 对象键(Key)是对象在存储桶中的唯一标识
     * @return
     */
    public COSObject getObject(String key) {
        return cosClient.getObject(cosClientConfig.getBucket(), key);
    }


    /**
     * 将本地图片上传到 COS 并且设置ci
     *
     * @param key  对象键(Key)是对象在存储桶中的唯一标识
     * @param file 本地文件
     * @return
     */
    public PutObjectResult putPictureObject(String key, File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(cosClientConfig.getBucket(), key, file);
        //对图像进行处理（获取图像的基本信息）
        PicOperations picOperations = new PicOperations();
        //1 表示返回原图信息
        picOperations.setIsPicInfo(1);
        //构造处理函数
        putObjectRequest.setPicOperations(picOperations);
        return cosClient.putObject(putObjectRequest);
    }


}
